package com.tom.org.service.impl;

import com.tom.basic.service.impl.BaseServiceImpl;
import com.tom.basic.utils.PageList;
import com.tom.org.domain.Department;
import com.tom.org.mapper.DepartmentMapper;
import com.tom.org.query.DepartmentQuery;
import com.tom.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class IDepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void save(Department department) {
     departmentMapper.save(department);

        String dirPath = "";
        if (department.getParent() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
    @Transactional
    @Override
    public void update(Department department) {
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
    }



 @Override
 public List<Department> tree() {
     // 查出所有数据
     List<Department> depts = departmentMapper.loadAll();
     // 这个集合用来储存排列完成的树数据
     List<Department> deptTree = new ArrayList<Department>();

     // 把集合数据转换为map
     HashMap<Long, Department> deptMap = new HashMap<>();
     for (Department department : depts) {
         deptMap.put(department.getId(), department);
     }
     // 从所有数据中找一级
     for (Department deptTmp : depts) {
      // 没有父 id 就是一级
      if (deptTmp.getParent_id() == null) {
       deptTree.add(deptTmp);
      } else {
       // 从map集合中获取到父对象，将自己添加到父对象的children集合中
       Department department = deptMap.get(deptTmp.getParent_id());
       if (department != null) {
        department.getChildren().add(deptTmp);
       }
      }
     }
     return deptTree;

    }
}
